package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsUserSign;
import com.caiyi.financial.nirvana.sms.querybean.SignQueryBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

@Mapper
public interface SmsUserSignMapper extends BaseMapper<SmsUserSign> {

    @Update("update sms_user_sign set defauls = 0  where account = #{account} and status = 2 ")
    int updateSignDefault(@Param("account") String account);

    @Update("update sms_user_sign  set status = #{status} , reason = #{reason},audit_time = now() where id = #{id} and status =1")
    int updateUserSignById(@Param("reason") String reason, @Param("status") Integer status, @Param("id") Integer id);

    @Delete("delete from sms_user_sign where account=#{account} and id = #{id} and defauls !=1")
    int delByAccountAndId(@Param("account") String account, @Param("id") Integer id);

    @Update("update sms_user_sign set defauls = 1 where account = #{account} and id = #{id} and status = 2 ")
    int updateSignByAccountAndId(@Param("id") Integer id, @Param("account") String account);

    @Select("select s.sign from sms_user_sign s where s.account = #{account} and  status = 2 order by defauls desc ")
    List<String> selectSignListByAccount(@Param("account") String account);
    @Select("select s.sign from sms_user_sign s where s.account = #{account} order by defauls desc ")
    List<String> selectSignListByAccounts(@Param("account") String account);
    @SelectProvider(type = UserSignSqlProvider.class, method = "selectSignListByParams")
    List<SmsUserSign> selectSignListByParams(SignQueryBean bean);

    class UserSignSqlProvider {
        public String selectSignListByParams(SignQueryBean bean) {
            return new SQL() {
                {
                    SELECT("*");
                    FROM("sms_user_sign t");
                    if (bean.getStatus() != null) {
                        WHERE("t.status =#{status}");
                    }
                    if (StringUtils.isNotBlank(bean.getAccount())) {
                        WHERE("t.account = #{account}");
                    }
                    if (bean.getDefauls()!=null) {
                        WHERE("t.defauls = #{defauls}");
                    }
                    if (StringUtils.isNotBlank(bean.getSign())) {
                        WHERE("t.sign LIKE concat('%',#{sign},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("t.create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("t.create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }

}
